package br.com.roteador.motorista;

import java.util.List;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import br.com.roteador.persistence.AbstractDAO;

@Named("motoristaDAO")
public class MotoristaDAO extends AbstractDAO {
	
	public MotoristaDAO (EntityManager manager){
		this.manager = manager;
	}

	public MotoristaDAO (){}
	
	private static final long serialVersionUID = -196738644541630320L;
	
	public List<Motorista> recuperaMotoristas() {
		CriteriaBuilder qb = manager.getCriteriaBuilder();
		CriteriaQuery<Motorista> cq = qb.createQuery(Motorista.class);
		Root<Motorista> tb = cq.from(Motorista.class);
		cq.select(tb);
		return (List<Motorista>) recuperaLista(montaQuery(cq));
	}

	public Motorista recuperaMotoristaPorNome(String nome) {
		CriteriaBuilder qb = manager.getCriteriaBuilder();
		CriteriaQuery<Motorista> cq = qb.createQuery(Motorista.class);
		Root<Motorista> tb = cq.from(Motorista.class);
		cq.select(tb);
		cq.where(qb.equal(tb.get("nome"), nome));
		return (Motorista) recuperaRegistro(montaQuery(cq));
	}
	
}